Getting Started with Golo
🚧 This is a work in progess
Hello World
hello.golo:
module hello.World
function main = |args| {
println("Hello, GoloScript! 👋")
}
run
golo hello.golo
Variables
Golo has two kinds of variables: immutable (let) and mutable (var). Variables declared with let cannot be reassigned after initialization — attempting to do so throws an exception at runtime. Use var when you need to reassign a value later.
variables.golo:
module getting.started.Variables
function main = |args| {
let immutable = 42 # Cannot change
try {
immutable = 100
} catch (e) {
println("😡: " + e)
}
var mutable = 42 # Can change
mutable = 100 # OK
println("Mutable variable changed to: " + mutable)
}
run
golo variables.golo
Output:
😡: Exception: cannot assign to immutable variable 'immutable' (declared with 'let')
Mutable variable changed to: 100
Functions
Functions in Golo are defined with the function keyword and take parameters between pipes (|...|). The last expression in a block is implicitly returned, so return is optional. You can also create lightweight closures with the arrow syntax (|x| -> expr).
functions.golo:
module getting.started.Functions
# Standard function
function add = |a, b| {
return a + b
}
# Implicit return
function multiply = |x, y| { x * y }
# No parameters
function greet = { "Hello!" }
function main = |args| {
let sum = add(3, 5)
println("Sum: " + sum)
let product = multiply(4, 6)
println("Product: " + product)
let message = greet()
println(message)
# Compact closure
let double = |x| -> x * 2
let value = 7
println("Double of " + value + " is " + double(value))
}
run
golo functions.golo
Variadic Functions (Varargs)
GoloScript supports variable arguments with the args… syntax (similar to JavaScript’s rest parameters).
varargs.golo:
module getting.started.Varargs
function test = |a, b...| {
println("a:", a)
println("b:", b)
println("type of b:", type(b))
println("length of b:", len(b))
return b
}
function main = |args| {
let result = test(1, 2, 3, 4)
println("result:", result)
println("result[0]:", result[0])
println("result[1]:", result[1])
}
run
golo varargs.goloOutput:
a: 1
b: [2, 3, 4]
type of b: ARRAY
length of b: 3
result: [2, 3, 4]
result[0]: 2
result[1]: 3